@using DotNetNuke.Services.Log.SiteLog;
@using DotNetNuke.Common;
@using System.Linq;
@using System.Collections;
@using System.Text;
@using DotNetNuke.Common.Utilities;
@using Personify.Modules.UserLocator;

@{  
    // make sure our module has necessary settings 
    if (string.IsNullOrEmpty(this._SettingsHelper("GoogleMapsKey")))
    {   
        //TODO: need to add error message if no key is defined             
    }
    
    string keys = string.Empty;    

    // try to retrieve the list of ip addresses from the data cache to save a hit on the SiteLog table
    List<UserLocatorIpInfo> items = UserLocatorHelper.GetIpData();
    
    if (items == null)
    { 
        items = this._GetUserHostAddresses();
        UserLocatorHelper.SetIpData(items);
    }
    
    StringBuilder keybuilder = new StringBuilder();
        
    // build an string of keys that we will use construct a javascript array
    for (int i = 0; i < items.Count; i++)
    {
        keybuilder.Append(string.Format("'{0}'", items[i].Token));
        if (i != items.Count - 1)
        { 
            keybuilder.Append(",");
        }
    }

    keys = keybuilder.ToString();
        
}
<script type="text/javascript">
    var keys = new Array(@keys);
</script>
<div id="map_canvas" style="width: 100%; height: 300px">
</div>
<script src="http://maps.google.com/maps/api/js?sensor=false&amp;key=@this._SettingsHelper("GoogleMapsKey")" type="text/javascript">
</script>
<script type="text/javascript">
    $(document).ready(function () {
        var myLatlng = new google.maps.LatLng(47.62, -122.36);
        var myOptions = {
            zoom: 2,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.SATELLITE
        }
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        $(keys).each(
            function () {
                var url = "@VirtualPathUtility.ToAbsolute("~/DesktopModules/Personify/UserLocator/LocationHandler.ashx?key=")" + this;
                $.getJSON(url, function (result) {
                    var latlng = new google.maps.LatLng(result.latitude, result.longitude);
                    var marker = new google.maps.Marker({
                        position: latlng,
                        map: map,
                        title: result.city + ', ' + result.regionname + ', ' + result.countryname
                    });
                });
            }
        );
    });
</script>
@functions{

    /// <summary>
    /// Helper function to get module settings.
    /// </summary>
    /// <param name="key">The setting name.</param>
    /// <returns>String version of the module setting.</returns>
    private string _SettingsHelper(string key)        
    {
        string retval = string.Empty;

        Hashtable settings = Dnn.Module.ModuleSettings;
        retval = (string)settings[key];
        
        return retval;
    }
    /// <summary>
    /// Gets the list of distinct user IP addresses from the site log with a date range 
    /// based on the module settings. 
    /// </summary>
    /// <returns></returns>
    private List<UserLocatorIpInfo> _GetUserHostAddresses()
    {
        List<UserLocatorIpInfo> items = new List<UserLocatorIpInfo>();

        DotNetNuke.Entities.Portals.PortalSettings ps = Globals.GetPortalSettings();
        SiteLogController slc = new SiteLogController();

        //TODO: set the start and end dates through module settings
        System.Data.IDataReader logs = slc.GetSiteLog(ps.PortalId, ps.PortalAlias.HTTPAlias, 2, DateTime.Now.AddDays(Convert.ToDouble(-1)), DateTime.Now);

        while (logs.Read())
        {
            if (!string.IsNullOrEmpty(logs.GetString(4)) && !items.Exists(i => i.IP == logs.GetString(4)))
            {
                items.Add(new UserLocatorIpInfo()
                {
                    IP = logs.GetString(4),
                    Token = Guid.NewGuid().ToString()
                });
            }
        }
        logs.Close();

        return items;
    }

}
